package com.blrs.service.project.implementation.impl;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import com.blrs.common.utils.DateUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.domain.ProjectBudget;
import com.blrs.domain.ProjectImplementation;
import com.blrs.domain.ProjectInitiation;
import com.blrs.domain.ProjectProcess;
import com.blrs.domain.dto.ProjectProcessInfoDTO;
import com.blrs.mapper.ProjectBudgetMapper;
import com.blrs.mapper.ProjectImplementationMapper;
import com.blrs.mapper.ProjectInitiationMapper;
import com.blrs.mapper.ProjectProcessMapper;
import com.blrs.service.project.implementation.IProjectImplementationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 项目实施Service业务层处理
 * 
 * @author guobin
 * @date 2025-01-10
 */
@Service
public class ProjectImplementationServiceImpl implements IProjectImplementationService
{
    @Autowired
    private ProjectImplementationMapper projectImplementationMapper;

    @Autowired
    private ProjectInitiationMapper projectInitiationMapper;

    @Autowired
    private ProjectProcessMapper projectProcessMapper;

    @Autowired
    private ProjectBudgetMapper projectBudgetMapper;

    /**
     * 查询项目实施
     * 
     * @param id 项目实施主键
     * @return 项目实施
     */
    @Override
    public ProjectImplementation selectProjectImplementationById(String id)
    {
        return projectImplementationMapper.selectProjectImplementationById(id);
    }

    /**
     * 查询项目实施列表
     * 
     * @param projectImplementation 项目实施
     * @return 项目实施
     */
    @Override
    public List<ProjectImplementation> selectProjectImplementationList(ProjectImplementation projectImplementation)
    {
        return projectImplementationMapper.selectProjectImplementationList(projectImplementation);
    }

    /**
     * 新增项目实施
     * 
     * @param projectImplementation 项目实施
     * @return 结果
     */
    @Override
    public int insertProjectImplementation(ProjectImplementation projectImplementation)
    {
        return projectImplementationMapper.insertProjectImplementation(projectImplementation);
    }

    /**
     * 修改项目实施
     * 
     * @param projectImplementation 项目实施
     * @return 结果
     */
    @Override
    public int updateProjectImplementation(ProjectImplementation projectImplementation)
    {
        projectImplementation.setUpdateTime(DateUtils.getNowDate());
        return projectImplementationMapper.updateProjectImplementation(projectImplementation);
    }

    /**
     * 批量删除项目实施
     * 
     * @param ids 需要删除的项目实施主键
     * @return 结果
     */
    @Override
    public int deleteProjectImplementationByIds(String[] ids)
    {
        return projectImplementationMapper.deleteProjectImplementationByIds(ids);
    }

    /**
     * 删除项目实施信息
     * 
     * @param id 项目实施主键
     * @return 结果
     */
    @Override
    public int deleteProjectImplementationById(String id)
    {
        return projectImplementationMapper.deleteProjectImplementationById(id);
    }

    @Override
    public int editProjectImplementation(ProjectProcessInfoDTO processInfoDTO) {
        int rows = 0;
        if(StringUtils.isNotEmpty(processInfoDTO.getProjectId())){
            ProjectInitiation initiation = projectInitiationMapper.selectProjectInitiationById(processInfoDTO.getProjectId());
            initiation.setActualStartTime(processInfoDTO.getActualStartTime());
            initiation.setActualEndTime(processInfoDTO.getActualEndTime());
            rows = projectInitiationMapper.updateProjectInitiation(initiation);
        }
        if(!processInfoDTO.getProcesses().isEmpty()){
            for (ProjectProcess process : processInfoDTO.getProcesses()) {
                ProjectProcess projectProcess = projectProcessMapper.selectProcessByProcessId(process.getProcessId());
                projectProcess.setActualStartTime(process.getActualStartTime());
                projectProcess.setActualEndTime(process.getActualEndTime());
                projectProcess.setStatus(process.getStatus());
                rows += projectProcessMapper.updateProjectProcess(projectProcess);
            }
        }
        return rows;
    }
}
